package web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.demo.trade.utils.Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import dto.CommentDto;
import entity.Admin;
import entity.Comment;
import entity.Oder;
import entity.User;
import enums.CommentEnum;
import enums.travelenums;
import service.Comment_service;
import service.Oder_service;

@Controller
public class Comment_controller {
	 	private static final Logger log=LoggerFactory.getLogger(Comment_controller.class);
	 	
		@Autowired
		private Comment_service comment_service;
		@Autowired
		private Oder_service oder_service;
		
		@RequestMapping(value="user/savecomment",method=RequestMethod.POST)
		@ResponseBody
		public String save_comment(@RequestParam("content")String content,
				@RequestParam("productId")Integer productId,
				@RequestParam("type")Integer type,
				@RequestParam("oderId")Integer oderId,HttpSession session) throws Exception{
			
			User user=(User)session.getAttribute("user");
			SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time=sd.format(new Date());
			
			Comment comment =new Comment(productId,user.getUsername(),content,null,type,time,time);
			Oder oder=oder_service.get_oder(user.getId(), oderId);
			if(oder!=null){
				comment_service.save_comment(comment);
				oder.setStatus(travelenums.COMMENTED.getState());
				oder_service.update_oder(oder);
				log.info("更新订单状态,评论成功");				
				return travelenums.SUCCESS.getInfo();
			}else{
				return travelenums.ERRO.getInfo(); 
			}			
		}
		
		@RequestMapping(value="showcomment",method=RequestMethod.GET)
		public String showComment(@RequestParam("productId")Integer productId,
						@RequestParam(value="type",required=false)Integer type,
						@RequestParam(value="page",defaultValue="1",required=true)Integer page,Model model){
			Page<Comment> pa=PageHelper.startPage(page, 6);
			if(type==null){
				List<Comment> list=comment_service.CommentList(productId,null);
				PageInfo<Comment> pageInfo=new PageInfo<Comment>(pa);
				model.addAttribute("pageInfo", pageInfo);
				model.addAttribute("list", list);
				return "comment";
			}
			List<Comment> list=comment_service.CommentList(productId,type);
			PageInfo<Comment> pageInfo=new PageInfo<Comment>(pa);
			model.addAttribute("pageInfo", pageInfo);
			model.addAttribute("list", list);
			return "comment";
		}
		
		/*后台*/
		
		@RequestMapping("commentlist")	//id是前端函数复用了(代表很多，可以是任何) index.js的方法
		public String CommentList(@RequestParam(value="page",defaultValue="1")Integer page,
								@RequestParam(value="id",required=false)Integer type,
								HttpSession session,Model model){
			Admin admin=(Admin)session.getAttribute("admin");
			if(admin!=null){
				Page<Comment> pa=PageHelper.startPage(page, 6);
				if(type==null){
					List<Comment> list=comment_service.CommentList(null,null);
					PageInfo<Comment> pageInfo=new PageInfo<Comment>(pa);
					model.addAttribute("page", pageInfo);
					model.addAttribute("list", list);
					return "back/comment";
				}else{
					List<Comment> list=comment_service.CommentList(null,type);
					PageInfo<Comment> pageInfo=new PageInfo<Comment>(pa);
					model.addAttribute("page", pageInfo);
					model.addAttribute("list", list);
					return "back/comment";
				}	
			}
			return travelenums.NOLOGIN.getInfo();
		}
		
		@RequestMapping(value="commentdetail")
		public String CommentDetail(@RequestParam("id")Integer id,
									HttpSession session,Model model){
			Admin admin=(Admin)session.getAttribute("admin");
			if(admin!=null){
				CommentDto commentDto=comment_service.getCommentDtoById(id);
				model.addAttribute("commentDto", commentDto);
				return "back/commentdetail";
			}
			return travelenums.NOLOGIN.getInfo();
		}
		
		@RequestMapping("delete/comment")
		@ResponseBody
		public String deleteComment(Integer id,HttpSession session){
			Admin admin=(Admin)session.getAttribute("admin");
			if(admin!=null){
				if(id!=null){
					 boolean boo=comment_service.remove_comment(id);
					 if(boo){
						 return travelenums.SUCCESS.getInfo();
					 }
					 return travelenums.ERRO.getInfo();
				}
				return travelenums.ERRO.getInfo();
			}
			return travelenums.NOLOGIN.getInfo();
		}
}
